package com.erp.dragGrid;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.erp.bean.WtPhotoBean;
import com.erp.Interface.OnDeleteInterface;
import com.erp.R;
import com.erp.util.Myutil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2017/9/19/019.
 * 作者：刘洋
 */

public class DragAdapter extends BaseAdapter {
    /** TAG*/
    private final static String TAG = "DragAdapter";
    private Context context;
    /** 控制的postion */
    private int holdPosition;
    /** 是否改变 */
    private boolean isChanged = false;
    /** 是否可见 */
    boolean isVisible = true;
    /** 可以拖动的列表（即用户选择的频道列表） */
    public List<WtPhotoBean> channelList;
    /** TextView 频道内容 */
    class HolderView {
        private LinearLayout llll;
        private ImageView iv_icon;
        private ImageView iv_delete;
        private TextView tv_GuiMing,tv_GuiHao;
    }
    public boolean isDelete = false;
    /** 要删除的position */
    public int remove_position = -1;

    private Handler mHandler = new Handler();

    private DragGrid grid;
    public List<Integer> listcb;
    public boolean isgai;
    /**
     * 指定隐藏的position
     */
    private int hideposition = -1;
    private OnDeleteInterface onDeleteInterface;
    public DragAdapter(Context context, List<WtPhotoBean> channelList, DragGrid grid, OnDeleteInterface onDeleteInterface) {
        this.context = context;
        this.channelList = channelList;
        this.grid = grid;
        this.onDeleteInterface=onDeleteInterface;
        this.listcb=new ArrayList<>();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return channelList == null ? 0 : channelList.size();
    }

    @Override
    public WtPhotoBean getItem(int position) {
        // TODO Auto-generated method stub
        if (channelList != null && channelList.size() != 0) {
            return channelList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        HolderView holderView = null;
        View view = null;
        if (view == null) {
            holderView = new HolderView();
            view = LayoutInflater.from(context).inflate(R.layout.vp_iv_photo_item, parent,false);
            holderView.llll= (LinearLayout) view.findViewById(R.id.edit_ll);
            holderView.iv_icon = (ImageView) view.findViewById(R.id.icon_iv);
            holderView.iv_delete = (ImageView) view.findViewById(R.id.iv_delet);
            holderView.tv_GuiHao= (TextView) view.findViewById(R.id.tv_GuiHao);
            holderView.tv_GuiMing= (TextView) view.findViewById(R.id.tv_GuiMing);
            ViewGroup.LayoutParams mLayoutParams = holderView.llll.getLayoutParams();
            mLayoutParams.width = Common.Width / 3;
            mLayoutParams.height = Common.Width / 3;
            holderView.llll.setLayoutParams(mLayoutParams);

            view.setTag(holderView);
        }else {
            holderView = (HolderView) view.getTag();
        }
        final WtPhotoBean wtPhotoBean = getItem(position);
        Myutil.settv(wtPhotoBean.c_name,holderView.tv_GuiMing);
        Myutil.settv(wtPhotoBean.cnum,holderView.tv_GuiHao);
        Myutil.showhttpphoto(wtPhotoBean.uri, holderView.iv_icon, context);
        holderView.iv_delete.setVisibility(View.GONE);
        for(int i=0;i<listcb.size();i++){
            if(listcb.get(i)==position){
                holderView.iv_delete.setVisibility(View.VISIBLE);
                break;
            }
        }
        if (position == getCount()-1){
            if (convertView == null) {
                convertView = view;
            }
            convertView.setEnabled(false);
            convertView.setFocusable(false);
            convertView.setClickable(false);
        }
        if(remove_position == position){
            deletInfo(position);
        }
        if (!isDelete) {
            holderView.iv_delete.setVisibility(View.GONE);
        }
        if (hideposition == position) {
            view.setVisibility(View.INVISIBLE);
        }else {
            view.setVisibility(View.VISIBLE);
        }
        return view;
    }

    public void shanchuxuan(int position) {
        if (!Common.isAnimaEnd) {
            return;
        }
        int xuan=getisxuan(position);
        if(xuan==-1) {
            listcb.add(position);
        }else{
            listcb.remove(xuan);
        }
    }

    public int getisxuan(int position){
        for(int i=0;i<listcb.size();i++){
            if(listcb.get(i)==position){
                return i;
            }
        }
        return -1;
    }
    public void isdelete(boolean isDelete){
        if(isDelete) {
            onDeleteInterface.ontuodong();
        }
        this.isDelete=isDelete;
    }
    public void isadd(int postition){
        listcb.add(postition);
    }
    /**
     * 删除某个position
     * @param position
     */
    public void deletInfo(int position)
    {
        channelList.remove(position);
        listcb.remove(getisxuan(position));
        hideposition = -1;
        isDelete=false;
        notifyDataSetChanged();
    }
    public void deletInfo(List<Integer> listvb) {
        Collections.sort(listvb);
        Collections.reverse(listvb);
        Log.d("TAAAG",listvb.toString());
        int b=0;
        for(int i=0;i<listvb.size();i++){
            int s=listvb.get(i);
//            b+=1;
//            s=s-b;
            Log.d("TAAAG","s="+s);
            channelList.remove(s);
            Log.d("TAAAG","size="+channelList.size());
        }
        isDelete=false;
        hideposition = -1;
        notifyDataSetChanged();
    }


    /** 添加频道列表 */
    public void addItem(WtPhotoBean channel) {
        channelList.add(channel);
        notifyDataSetChanged();
    }

    /** 拖动变更频道排序 */
    public void exchange(int dragPostion, int dropPostion) {
        holdPosition = dropPostion;
        WtPhotoBean dragItem = getItem(dragPostion);
        Log.d(TAG, "startPostion=" + dragPostion + ";endPosition=" + dropPostion);
        if (dragPostion < dropPostion) {
            channelList.add(dropPostion + 1, dragItem);
            channelList.remove(dragPostion);
        } else {
            channelList.add(dropPostion, dragItem);
            channelList.remove(dragPostion + 1);
        }
        boolean is1=false;
        for(int i=0;i<listcb.size();i++){
            if(listcb.get(i)==dragPostion){
                listcb.remove(i);
                listcb.add(dropPostion);
                is1=true;
                return;
            }
        }
        if(is1) {
            for (int i = 0; i < listcb.size() - 1; i++) {
                if (listcb.get(i) == dropPostion) {
                    listcb.remove(i);
                    listcb.add(dragPostion);
                    return;
                }
            }
        }else{
            for (int i = 0; i < listcb.size(); i++) {
                if (listcb.get(i) == dropPostion) {
                    listcb.remove(i);
                    listcb.add(dragPostion);
                    return;
                }
            }
        }
        isChanged = true;
        notifyDataSetChanged();
    }

    /** 设置删除的position */
    public void setRemove(int position) {
        remove_position = position;
        notifyDataSetChanged();
    }

    /** 获取是否可见 */
    public boolean isVisible() {
        return isVisible;
    }

    /** 设置是否可见 */
    public void setVisible(boolean visible) {
        isVisible = visible;
    }
    /** 显示放下的ITEM */

    public void setHidePosition(int position) {
        // TODO Auto-generated method stub
        this.hideposition = position;
        notifyDataSetChanged();
    }
}
